package com.example.getpixel;


import java.util.Random;
import java.util.Vector;

import android.R.color;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	
	final Context context = this;
	
	public Vector<Integer> checkQuiz = new Vector<Integer>();
	String[] setQuiz = { "2,4,3,6,5,0,1,9,8,7", "0,2,4,9,1,6,3,5,7,8",
			"6,9,7,8,5,2,4,0,3,1", "3,6,0,8,5,2,1,4,9,7",
			"7,0,1,3,6,2,4,9,5,8", "5,3,4,8,7,1,6,0,2,9",
			"1,0,2,7,8,9,3,6,4,5", "8,1,3,9,5,2,7,0,6,4",
			"4,9,1,5,8,3,7,0,6,2", "4,8,1,2,3,6,7,0,5,9" };
	
	Random r = new Random();
	DataBase myquiz = new DataBase(context);
	Vector<getQuiz> quiz_list = new Vector<getQuiz>();
	int width,height;
	String[] selectQuiz;
	String ans1,ans2,ans3,ans4;
	int[] targetNumber = new int[10];
	
	int chooseQuiz = 0;
	int round = 0;
	int Low = 0;
	int High = 10;
	
	String answer = null;
	
	boolean isClick = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Hello DataBase", "Data : 0");
        ans1 = null;
        ans2 = null;
        ans3 = null;
        ans4 = null;
        
        myquiz.getWritableDatabase();
 		myquiz.createTableQuiz();
 		myquiz.createTableColor();
 		myquiz.getAllQuiz();
 		myquiz.getAllColor();
 	
        selectQuizs();
        
        Button btn0 = (Button)findViewById(R.id.buttonNum0);
        Button btn1 = (Button)findViewById(R.id.buttonNum1);
        Button btn2 = (Button)findViewById(R.id.buttonNum2);
        Button btn3 = (Button)findViewById(R.id.buttonNum3);
        Button btn4 = (Button)findViewById(R.id.buttonNum4);
        Button btn5 = (Button)findViewById(R.id.buttonNum5);
        Button btn6 = (Button)findViewById(R.id.buttonNum6);
        Button btn7 = (Button)findViewById(R.id.buttonNum7);
        Button btn8 = (Button)findViewById(R.id.buttonNum8);
        Button btn9 = (Button)findViewById(R.id.buttonNum9);
        Button btnAns = (Button)findViewById(R.id.ButtonAnswer);
        
        btn0.setBackgroundColor(Color.WHITE);
        btn1.setBackgroundColor(Color.WHITE);
        btn2.setBackgroundColor(Color.WHITE);
        btn3.setBackgroundColor(Color.WHITE);
        btn4.setBackgroundColor(Color.WHITE);
        btn5.setBackgroundColor(Color.WHITE);
        btn6.setBackgroundColor(Color.WHITE);
        btn7.setBackgroundColor(Color.WHITE);
        btn8.setBackgroundColor(Color.WHITE);
        btn9.setBackgroundColor(Color.WHITE);
        
        btnAns.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				MediaPlayer mpEffect = MediaPlayer.create(MainActivity.this, R.raw.button_click);		
				mpEffect.start();
				// TODO Auto-generated method stub
				EditText textAnswer = (EditText)findViewById(R.id.editTextAnswer);
				textAnswer.setRawInputType(Configuration.KEYBOARD_QWERTY);
				answer = textAnswer.getText().toString();
//				String test = "Answer : "+answer.charAt(0);
//				Toast toast = Toast.makeText(getApplicationContext(), test, Toast.LENGTH_LONG);
//				toast.show();
				if(!answer.matches("") && answer.length()==1){
					if(answer.charAt(0)=='0'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_01);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_02);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_01);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_02);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='1'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_11);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_12);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_11);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_12);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='2'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_21);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_22);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_21);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_22);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='3'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_31);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_32);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_31);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_32);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='4'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_41);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_42);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_41);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_42);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='5'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_51);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_52);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_51);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_52);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='6'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_61);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_62);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_61);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_62);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='7'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_71);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_72);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_71);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_72);
						isClick = true;
						alertDialogShow();
					}
					else if(answer.charAt(0)=='8'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_81);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_82);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_81);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_82);
						isClick = true;
						alertDialogShow();
					}
					else if (answer.charAt(0)=='9'){
						ans1 = getNumberColorPixel(R.drawable.nawhiteblur_91);
						ans2 = getNumberColorPixel(R.drawable.nawhiteblur_92);
						ans3 = getNumberColorPixel(R.drawable.whiteblur_91);
						ans4 = getNumberColorPixel(R.drawable.whiteblur_92);
						isClick = true;
						alertDialogShow();
					}
					else{
						Toast toast2 = Toast.makeText(getApplicationContext(), "Please input only 0-9!!", Toast.LENGTH_LONG);
						toast2.show();
						textAnswer.setText("");
					}
				}
				else{
					Toast toast2 = Toast.makeText(getApplicationContext(), "Please input only 0-9!!", Toast.LENGTH_LONG);
					toast2.show();
				}
			}
		});


    }

public void showColorAnswer(){
	Button btn0 = (Button)findViewById(R.id.buttonNum0);
    Button btn1 = (Button)findViewById(R.id.buttonNum1);
    Button btn2 = (Button)findViewById(R.id.buttonNum2);
    Button btn3 = (Button)findViewById(R.id.buttonNum3);
    Button btn4 = (Button)findViewById(R.id.buttonNum4);
    Button btn5 = (Button)findViewById(R.id.buttonNum5);
    Button btn6 = (Button)findViewById(R.id.buttonNum6);
    Button btn7 = (Button)findViewById(R.id.buttonNum7);
    Button btn8 = (Button)findViewById(R.id.buttonNum8);
    Button btn9 = (Button)findViewById(R.id.buttonNum9);
    
	for(int i = 0;i<targetNumber.length;i++){
		showTrueAnswer();
    	if(targetNumber[i]>970){
    		if(ans1 == "Correct" && ans2 == "Correct" && ans3 == "Correct" && ans4 == "Correct"){
    			MediaPlayer mpEffect = MediaPlayer.create(MainActivity.this,
						R.raw.correct);
				mpEffect.start();
	    		if(i==0){
	    			btn0.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==1){
	    			btn1.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==2){
	    			btn2.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==3){
	    			btn3.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==4){
	    			btn4.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==5){
	    			btn5.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==6){
	    			btn6.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==7){
	    			btn7.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==8){
	    			btn8.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		else if(i==9){
	    			btn9.setBackgroundColor(Color.rgb(173,255,47));
	    		}
	    		
    		}
    		else if (ans1 == "Wrong" && ans2 == "Wrong" && ans3 == "Wrong" && ans4 == "Wrong"){
    			
    			MediaPlayer mpEffect = MediaPlayer.create(MainActivity.this,
						R.raw.wrong);
				mpEffect.start();
    			
    			if(i==0){
        			btn0.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==1){
        			btn1.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==2){
        			btn2.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==3){
        			btn3.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==4){
        			btn4.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==5){
        			btn5.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==6){
        			btn6.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==7){
        			btn7.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==8){
        			btn8.setBackgroundColor(Color.rgb(255,99,71));
        		}
        		else if(i==9){
        			btn9.setBackgroundColor(Color.rgb(255,99,71));
        		}
    			
    		}
    	}
    	else {
    			if(i==0){
        			btn0.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==1){
        			btn1.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==2){
        			btn2.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==3){
        			btn3.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==4){
        			btn4.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==5){
        			btn5.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==6){
        			btn6.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==7){
        			btn7.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==8){
        			btn8.setBackgroundColor(Color.rgb(251,255,190));
        		}
        		else if(i==9){
        			btn9.setBackgroundColor(Color.rgb(251,255,190));
        		}
    	}
    }
	waitForSelectQuiz();
}

public void clearColor(){
	Button btn0 = (Button)findViewById(R.id.buttonNum0);
    Button btn1 = (Button)findViewById(R.id.buttonNum1);
    Button btn2 = (Button)findViewById(R.id.buttonNum2);
    Button btn3 = (Button)findViewById(R.id.buttonNum3);
    Button btn4 = (Button)findViewById(R.id.buttonNum4);
    Button btn5 = (Button)findViewById(R.id.buttonNum5);
    Button btn6 = (Button)findViewById(R.id.buttonNum6);
    Button btn7 = (Button)findViewById(R.id.buttonNum7);
    Button btn8 = (Button)findViewById(R.id.buttonNum8);
    Button btn9 = (Button)findViewById(R.id.buttonNum9);
    
    btn0.setBackgroundColor(Color.WHITE);
    btn1.setBackgroundColor(Color.WHITE);
    btn2.setBackgroundColor(Color.WHITE);
    btn3.setBackgroundColor(Color.WHITE);
    btn4.setBackgroundColor(Color.WHITE);
    btn5.setBackgroundColor(Color.WHITE);
    btn6.setBackgroundColor(Color.WHITE);
    btn7.setBackgroundColor(Color.WHITE);
    btn8.setBackgroundColor(Color.WHITE);
    btn9.setBackgroundColor(Color.WHITE);
}

public void showTrueAnswer(){
	
	Button btn0 = (Button)findViewById(R.id.buttonNum0);
    Button btn1 = (Button)findViewById(R.id.buttonNum1);
    Button btn2 = (Button)findViewById(R.id.buttonNum2);
    Button btn3 = (Button)findViewById(R.id.buttonNum3);
    Button btn4 = (Button)findViewById(R.id.buttonNum4);
    Button btn5 = (Button)findViewById(R.id.buttonNum5);
    Button btn6 = (Button)findViewById(R.id.buttonNum6);
    Button btn7 = (Button)findViewById(R.id.buttonNum7);
    Button btn8 = (Button)findViewById(R.id.buttonNum8);
    Button btn9 = (Button)findViewById(R.id.buttonNum9);
    
    String[] arrayAnawer = quiz_list.get(Integer.parseInt(selectQuiz[round-1])).getAnswer().split("-");
    
    if(Integer.parseInt(arrayAnawer[0])==0){
		btn0.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==1){
		btn1.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==2){
		btn2.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==3){
		btn3.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==4){
		btn4.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==5){
		btn5.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==6){
		btn6.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==7){
		btn7.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==8){
		btn8.setBackgroundColor(Color.rgb(173,255,47));
	}
	else if(Integer.parseInt(arrayAnawer[0])==9){
		btn9.setBackgroundColor(Color.rgb(173,255,47));
	}    
}
    
 public void alertDialogShow(){
	 new CountDownTimer(3000, 1000) {

			@Override
			public void onFinish() {
				if(isClick){
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
							context);

					// set title
					alertDialogBuilder.setTitle("Your Answer.");

					// set dialog message
					alertDialogBuilder
							.setMessage("Answer : "+answer+" , [ white light : "+ans3+" , "+ans4+" ] , [ Natural light : "+ans1+" , "+ans2+" ]")
							.setCancelable(false)
							.setPositiveButton("Ok",
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog,
												int id) {
										}

									});

					// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();

				
					// show it
					alertDialog.show();
					showColorAnswer();
					EditText textAnswer = (EditText)findViewById(R.id.editTextAnswer);
					textAnswer.setText("");
				}
			}

			@Override
			public void onTick(long millisUntilFinished) {
			}

		}.start();
 }
    
 public void waitForSelectQuiz() {
		// TODO Auto-generated method stub
	 new CountDownTimer(5000, 1000) {

			@Override
			public void onFinish() {
				selectQuizs();
				clearColor();
			}

			@Override
			public void onTick(long millisUntilFinished) {

			}

		}.start();
	}



// select quiz
 	public void selectQuizs() {
 		
 		quiz_list = myquiz.getAllQuiz();

 		// set textView and ImageView
 		ImageView quiz = (ImageView) findViewById(R.id.imageNumRandom);
 		
 		Resources resources = this.getResources();
 		int resourceID;
 		
 		if (round == 0)
			chooseQuiz = r.nextInt(High - Low) + Low;

 		// it will random set of quiz at first play and will random the set of
 		// quiz again if the player answer 10 quiz
 		

 		selectQuiz = setQuiz[chooseQuiz].split(",");

 		// check that round is not more than 10 quiz , if not it will show alert
 		// dialog and set round , score to 0
 		if (round < selectQuiz.length) {
 			Log.i("Total", "R,G,B - Quiz : " + round);
 			
 			// get the quiz picture that has select in floder drawable and show it.
 			resourceID = resources.getIdentifier(
 					quiz_list.get(Integer.parseInt(selectQuiz[round]))
 							.getQuiz(), "drawable",
 					"com.example.getpixel");
 			quiz.setImageResource(resourceID);
 			round = round + 1;
 		} else {

 			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
 					context);

 			// set title
 			alertDialogBuilder.setTitle("Finish");

 			// set dialog message
 			alertDialogBuilder
 					.setMessage("Thank You!! Your Srore is : " )
 					.setCancelable(false)
 					.setPositiveButton("Main Menu",
 							new DialogInterface.OnClickListener() {
 								public void onClick(DialogInterface dialog,
 										int id) {
 									// if this button is clicked, just close
 									// the dialog box and do nothing
 									finish();
 									System.exit(0);
 								}

 							});

 			// create alert dialog
 			AlertDialog alertDialog = alertDialogBuilder.create();

 		
 			// show it
 			alertDialog.show();
 		}
 	}
 	
 	public String getNumberColorPixel(int pic){
 		String ans = "Correct";
 		Vector<getColor> color_list = myquiz.getAllColor();
 		Vector<Integer> answer = new Vector<Integer>();
 		
 		for (int i = 0; i < targetNumber.length; i++) {
			targetNumber[i] = 0;
		}
 		Context context = getApplicationContext();
        Drawable drawable = context.getResources().getDrawable(pic);
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        int x,y;
        
        for(x=10;x<bitmap.getWidth();x++){
        	for(y=60;y<bitmap.getHeight()/2;y++){
        		int pixel = bitmap.getPixel(x,y);
        		int redValue = Color.red(pixel);
        		int blueValue = Color.blue(pixel);
        		int greenValue = Color.green(pixel);
        		
        		for(int i = 0;i<color_list.size();i++){
        			if((color_list.get(i).getMinRed() <= redValue) && (redValue <= color_list.get(i).getMaxRed()) && (color_list.get(i).getMinGreen() <= greenValue) && (greenValue <= color_list.get(i).getMaxGreen()) && (color_list.get(i).getMinBlue() <= blueValue) && (blueValue <= color_list.get(i).getMaxBlue())){
        				targetNumber[i]++;
        			}
        		}
        	}
        }
        
        String[] arrayAnawer = quiz_list.get(Integer.parseInt(selectQuiz[round-1])).getAnswer().split("-");
        
        for(int i = 0;i<targetNumber.length;i++){
        	if(targetNumber[i]>970){
        		answer.add(i);
        	}
        }
        
        if(answer.size() > 1 || answer.size() == 0){
        	ans = "Wrong";
        }
        else{
        	if(answer.get(0) == Integer.parseInt(arrayAnawer[0])){
        		ans = "Correct";
        	}
        	else
        		ans = "Wrong";
        }
        
        return ans;
 	}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
